package com.bjesc_b2c.app.appraiset.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.CarSourcePhotoAdapter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.CarSourcePhotoPersnter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.CarSourcePhotoPersenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CarSourcePhotoView;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.BigCarSourcePhotoActivity;
import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import master_bean.CarSourcePhotoBean;
import rx.Subscription;

/**
 * Created by yidong on 2017/5/31.
 */

public abstract class BaseCarInfoImageFragment extends BaseFragment implements CarSourcePhotoView {

    @InjectView(R.id.banner)
    Banner banner;
    protected CarSourcePhotoAdapter photoAdapter;
    protected List<CarSourcePhotoBean> photoBeanList = new ArrayList<>();
    protected int currentIndex;
    protected Intent bigPhotoIntent;

    protected CarSourcePhotoPersnter persnter;
    private Subscription subscription;
    protected List<String> fileList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.banner_viewpager, null);
        ButterKnife.inject(this, contentView);
        initRxBus(setOnNext());
        persnter = new CarSourcePhotoPersenterImpl(this, getActivity());
        bigPhotoIntent = new Intent(getActivity(), BigCarSourcePhotoActivity.class);//跳转到大图
        if (!TextUtils.isEmpty(getSourceID())) {
            persnter.getPhoto(getSourceID());
        } else {
            showToast("分类ID为空");
        }
        return contentView;

    }

    protected abstract String getSourceID();

    protected abstract void getSourceList();

    protected void initViews() {
        List<String> imageUrls = new ArrayList<>();
        List<String> imageTitle = new ArrayList<>();
        for (int i = 0; i < photoBeanList.size()-1; i++) {//-1为了不显示更多里面的图片
            String image = photoBeanList.get(i).getPhoto();
            imageTitle.add(photoBeanList.get(i).getPhotoName());
            Logger.d(image);
            imageUrls.add(image);
        }
        //设置banner样式（显示圆形指示器和标题）
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerTitles(imageTitle);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //新版的banner的使用----偷下懒的使用方法

        banner.setImages(imageUrls).setImageLoader(new ImageLoaderInterface() {
            @Override
            public void displayImage(Context context, Object path, View imageView) {

                Glide.with(context)
                        .load(path)
                        .placeholder(R.mipmap.ic_placeholder_small)
                        .error(R.mipmap.ic_loadding_error_small) // will be displayed if the image cannot be loaded
                        .into((ImageView) imageView);

            }
            @Override
            public View createImageView(Context context) {
                return null;
            }
        }).start();
    }


    @Override
    public void getPhoto(List<CarSourcePhotoBean> photoBeanList) {
        if (photoBeanList != null && photoBeanList.size() > 0) {
            this.photoBeanList = photoBeanList;
        } else {
            getSourceList();
        }
        initViews();
    }

    @Override
    public void getFileName(List<String> fileList) {
        if(fileList!= null && fileList.size() > 0){
            this.fileList = fileList;
        }
    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        if (subscription != null) {
            subscription.unsubscribe();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

}